﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web.UI;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectCommentsActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {

            if (String.IsNullOrEmpty(request.SendValue) == false)
            {
                this.Context.Send(new UISectionBuilder(request.Model, request.Command, new UMC.Web.WebMeta().Put("Id", request.SendValue))

                        .Builder(), true);
            }
            var user = this.Context.Token.Identity();
            var refer_id = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", "0"));


            var IsAdmin = false;

            if (refer_id != 0)
            {
                var subject = DataFactory.Instance().Subject(refer_id.Value);
                if (subject != null && subject.project_id.HasValue)
                {
                    var member = DataFactory.Instance().ProjectMember(subject.project_id.Value, user.Id.Value);
                    if (member != null)
                    {
                        switch (member.AuthType)
                        {
                            case WebAuthType.Admin:
                                IsAdmin = true;
                                break;
                        }
                    }
                }
            }
            var type = this.AsyncDialog("type", g => this.DialogValue("Desc"));

            var cmId = this.AsyncDialog("CommentId", r =>
            {


                var form = request.SendValues ?? new UMC.Web.WebMeta();

                int limit = UMC.Data.Utility.IntParse(form["limit"] as string, type == "Asc" ? 5 : 25);

                int start = UMC.Data.Utility.IntParse(form["start"] as string, 0);
                var search = new Comment { for_id = 0 };

                if (refer_id == 0)
                {
                    search.user_id = user.Id;

                }
                else
                {

                    search.ref_id = refer_id;
                }

                int next;//= 0;
                var cms = DataFactory.Instance().Search(search, type == "Asc", start, out next, limit);
                var ui = UISection.Create();
                ui.Title = new UITitle("评论");
                var cells = Utility.Comments(cms, request.Model, request.Url);
                ui.Key = "Comments";
                if (cms.Length == 0)
                {
                    ui.IsNext = false;
                    ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "暂无评论").Put("icon", "\uF0E6"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),

                        new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));
                }
                else
                {

                    if (request.IsCashier || IsAdmin)
                    {
                        foreach (var cell in cells)
                        {

                            ui.Delete(cell, new UIEventText().Click(new UIClick(request, r, cell.Id, "type", "Del")));
                        }
                    }
                    else
                    {

                        ui.AddCells(cells.ToArray());
                    }
                    ui.StartIndex = next;
                    ui.IsNext = next > 0;
                }
                response.Redirect(ui);
            });
            switch (type)
            {
                case "Del":
                    if (request.IsCashier || IsAdmin)
                    {
                        var cId = UMC.Data.Utility.UInt64Parse(cmId) ?? 0;
                        DataFactory.Instance().Delete(new Comment
                        {
                            Id = cId
                        });

                    }
                    break;
            }

        }



    }
}